package cn.itrip.controller;

import cn.itrip.common.DateUtil;
import cn.itrip.common.DtoUtil;
import cn.itrip.common.EmptyUtils;
import cn.itrip.dto.Dto;
import cn.itrip.pojo.ItripAreaDic;
import cn.itrip.pojo.ItripHotelRoom;
import cn.itrip.pojo.ItripImage;
import cn.itrip.pojo.ItripLabelDic;
import cn.itrip.service.itripAreaDic.ItripAreaDicService;
import cn.itrip.service.itripHotelRoom.ItripHotelRoomService;
import cn.itrip.service.itripImage.ItripImageService;
import cn.itrip.service.itripLabelDic.ItripLabelDicService;
import cn.itrip.vo.ItripAreaDicVO;
import cn.itrip.vo.ItripImageVO;
import cn.itrip.vo.ItripLabelDicVO;
import cn.itrip.vo.hotelroom.ItripHotelRoomVO;
import cn.itrip.vo.hotelroom.SearchHotelRoomVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

@Controller
@RequestMapping("/api")
public class ItripHotelController {

    @Resource
    private ItripLabelDicService labelDicService;
    @Resource
    private ItripAreaDicService itripAreaDicService;
    @Resource
    private ItripHotelRoomService itripHotelRoomService;
    @Resource
    private ItripImageService itripImageService;

    //http://www.itrip.com/biz/api/hotelroom/getimg/74
    @ResponseBody
    @RequestMapping("/hotelroom/getimg/{id}")
    public Dto<ItripImageVO> getImg(@PathVariable Long id){
        List<ItripImageVO>  imageVOList = new ArrayList<>();

        Map<String,Object> map = new HashMap<>();
        map.put("targetId",id);
        map.put("type",1);
        try {
            List<ItripImage> itripImageList= itripImageService.getItripImageListByMap(map);

            if (EmptyUtils.isEmpty(itripImageList)){
                return DtoUtil.returnFail("查询图片失败","100007");
            }else{
                for (ItripImage itripImage : itripImageList) {
                    ItripImageVO itripImageVO = new ItripImageVO();
                    BeanUtils.copyProperties(itripImage,itripImageVO);
                    imageVOList.add(itripImageVO);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return DtoUtil.returnDataSuccess(imageVOList);
    }
    /**
     * 查询房型列表
     * http://www.itrip.com/biz/api/hotelroom/queryhotelroombyhotel
     * endDate: "2021-03-24T09:42:23.000Z"
     * hotelId: "4"
     * isBook: 1
     * isCancel: 1
     * isHavingBreakfast: 1
     * isTimelyResponse: 1
     * payType: "1"
     * roomBedTypeId: "2"
     * startDate: "2021-03-11T09:42:23.000Z"
     *
     */
    @ResponseBody
    @RequestMapping(value = "/hotelroom/queryhotelroombyhotel",method = RequestMethod.POST,produces = "application/json")
    public Dto<ItripHotelRoomVO> queryHotelRoomByHotel(@RequestBody SearchHotelRoomVO searchHotelRoomVO){

        Map<String,Object> map = new HashMap<>();
        // 数据打包
        List<List<ItripHotelRoomVO>> HotelRoomVOList = new ArrayList<List<ItripHotelRoomVO>>();
        // 1.判断入住时间和退房时间不能为空
        if (EmptyUtils.isNotEmpty(searchHotelRoomVO.getStartDate()) || EmptyUtils.isNotEmpty(searchHotelRoomVO.getEndDate())) {

            // 2.判断入住时间要小于 退房时间
            if (searchHotelRoomVO.getStartDate().getTime() > searchHotelRoomVO.getEndDate().getTime()){
                return DtoUtil.returnFail("入住时间不能大于退房时间!","100004");
            }

            List<Date> timeList = DateUtil.getBetweenDates(searchHotelRoomVO.getStartDate(), searchHotelRoomVO.getEndDate());
            map.put("timeList",timeList);
            map.put("hotelId",searchHotelRoomVO.getHotelId());
            map.put("isBook",searchHotelRoomVO.getIsBook());
            map.put("isCancel",searchHotelRoomVO.getIsCancel());
            map.put("isHavingBreakfast",searchHotelRoomVO.getIsHavingBreakfast());
            map.put("isTimelyResponse",searchHotelRoomVO.getIsTimelyResponse());
            map.put("payType",searchHotelRoomVO.getPayType());
            map.put("roomBedTypeId",searchHotelRoomVO.getRoomBedTypeId());
            try {
                List<ItripHotelRoom> itripHotelRoomList = itripHotelRoomService.getItripHotelRoomListByMap(map);
                for (ItripHotelRoom itripHotelRoom : itripHotelRoomList) {
                    ItripHotelRoomVO itripHotelRoomVO = new ItripHotelRoomVO();
                    BeanUtils.copyProperties(itripHotelRoom,itripHotelRoomVO);
                    // 创建新列表
                    List<ItripHotelRoomVO> itripHotelRoomVOList = new ArrayList<ItripHotelRoomVO>();
                    // 将数据添加到列表中
                    itripHotelRoomVOList.add(itripHotelRoomVO);
                    // 将列表中的数据添加到大列表中
                    HotelRoomVOList.add(itripHotelRoomVOList);

                }

            } catch (Exception e) {
                e.printStackTrace();
                return DtoUtil.returnFail("查询房型详情失败!","100006");
            }
        }else{
            return DtoUtil.returnFail("入住时间和退房时间不能为空!","100005");
        }

        return DtoUtil.returnDataSuccess(HotelRoomVOList);
    }

    // http://www.itrip.com/biz/api/hotelroom/queryhotelroombed

    /**
     * 查询所有床型
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/hotelroom/queryhotelroombed",method = RequestMethod.GET)
    public Dto<List<ItripLabelDicVO>> queryHotelRoombed(){
        List<ItripLabelDicVO> itripLabelDicVOList = new ArrayList<>();
        ItripLabelDicVO itripLabelDicVO = null;
        Map<String,Object> map = new HashMap<>();
        map.put("parentId",1);
        try {
            List<ItripLabelDic> itripLabelDicList = labelDicService.getItripLabelDicListByMap(map);
            for (ItripLabelDic itripLabelDic : itripLabelDicList) {
                itripLabelDicVO = new ItripLabelDicVO();
                BeanUtils.copyProperties(itripLabelDic,itripLabelDicVO);
                itripLabelDicVOList.add(itripLabelDicVO);
            }
        } catch (Exception e) {
            e.printStackTrace();
            DtoUtil.returnFail("查询床型失败","100004");
        }
        return DtoUtil.returnDataSuccess(itripLabelDicVOList);
    }


    //http://www.itrip.com/biz/api/hotel/queryhotcity/2

    /**
     * 查询热门城市
     * @param isChina
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/hotel/queryhotcity/{isChina}",method = RequestMethod.GET,produces = "application/json")
    public Dto<List<ItripAreaDicVO>> queryHotCity(@PathVariable Integer isChina){
        // 判断参数是否为空
        if (EmptyUtils.isNotEmpty(isChina)){
            List<ItripAreaDicVO> itripAreaDicVOList  = new ArrayList<>();
            Map<String,Object> map = new HashMap<>();
            map.put("isChina",isChina);// 1:国内 2:国外
            map.put("isHot",1);// 1:热门城市 0:不是热门
            try {
                List<ItripAreaDic> itripAreaDicList = itripAreaDicService.getItripAreaDicListByMap(map);
                for (ItripAreaDic itripAreaDic : itripAreaDicList) {
                    ItripAreaDicVO itripAreaDicVO =  new ItripAreaDicVO();
                    // 将数据导入vo中
                    BeanUtils.copyProperties(itripAreaDic,itripAreaDicVO);
                    // 加入列表
                    itripAreaDicVOList.add(itripAreaDicVO);
                }
            } catch (Exception e) {
                e.printStackTrace();
                return DtoUtil.returnFail("查询热门城市失败","100002");
            }
            return DtoUtil.returnDataSuccess(itripAreaDicVOList);
        }else{
            return DtoUtil.returnFail("国内外标识不能为空","100003");
        }

    }
    //http://localhost:8080/biz/api/hotel/queryhotelfeature
    //http://localhost:8081/biz/api/hotel/queryhotelfeature
    // nginx

    /**
     *  查询酒店特色
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/hotel/queryhotelfeature",method = RequestMethod.GET,produces = "application/json")
    public Dto<List<ItripLabelDicVO>> queryHotelFeature(){
        List<ItripLabelDicVO>  labelDicVOList = new ArrayList<>();
        Map<String,Object> map = new HashMap<>();
        map.put("parentId",16);
        // 查询特色酒店列表
        try {
            List<ItripLabelDic> labelDicList = labelDicService.getItripLabelDicListByMap(map);
            // 循环将数据转到vo里面
            for (ItripLabelDic itripLabelDic : labelDicList) {
                ItripLabelDicVO itripLabelDicVO = new ItripLabelDicVO();
                // 将查询到的资源赋值到vo中
                BeanUtils.copyProperties(itripLabelDic,itripLabelDicVO);
                labelDicVOList.add(itripLabelDicVO);
            }
        } catch (Exception e) {
            e.printStackTrace();
            // 系统异常
            DtoUtil.returnFail("查询酒店特色列表失败!","100001");
        }

        return DtoUtil.returnDataSuccess(labelDicVOList);
    }
}
